﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Sponsor {

    public class SponsorEnrollModel : ApplicationModel {
        
        #region Private Properties

        private Core.Sponsor.Sponsor sponsor = null;

        #endregion


        #region Public Properties

        public Int64 SponsorId {

            set {

                sponsor = Application.Core.Sponsor.Sponsor (value);

                if (sponsor == null) {

                    sponsor = new Core.Sponsor.Sponsor (Application);

                }

            }

        }

        public Core.Sponsor.Sponsor Sponsor { get { return sponsor; } set { sponsor = value; } }

        #endregion 


        #region Constructors

        public SponsorEnrollModel (Int64 forSponsorId) {

            SponsorId = forSponsorId;

            return;

        }

        public SponsorEnrollModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }


        public System.Web.Mvc.SelectList EnrollmentReasonSelectList {

            get {

                List<Core.General.ActionReason> reasons;

                reasons = (from currentActionReason in Application.Core.General.ActionReasons (true)

                           where ((currentActionReason.Enabled) 
                            
                                && (currentActionReason.EntityType == Core.Entity.Enumerations.EntityType.Sponsor) 
                                
                                && (currentActionReason.ActionType == Core.General.Enumerations.ActionType.EnrollmentPositive))

                           select currentActionReason).ToList ();

                return new System.Web.Mvc.SelectList (reasons, "Id", "Name");

            }

        }
        
        public System.Web.Mvc.SelectList ProgramSelectList {

            get {

                List<Core.Insurer.Program> programs;

                programs = (from currentProgram in Application.Core.Insurer.Programs (false)

                            where (currentProgram.Enabled) // ACTIVE OR IN USE 

                            select currentProgram).ToList ();

                return new System.Web.Mvc.SelectList (programs, "Id", "Name");

            }

        }
        
        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);


            // RELOAD BASE OBJECT FROM DATABASE 

            SponsorId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.Id");
                        

            return;

        }

        public Boolean Enroll (System.Collections.Specialized.NameValueCollection form) {

            Boolean success = false;


            // READ IN ALL VALUES FROM POSTED FORM 

            DateTime? coverageEffectiveDate = Data.Readers.FormReader.DateTimeFromDatabaseAllowNull (form, "Sponsor.Enrollment.EffectiveDate");

            Int64 enrollmentActionReasonId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.Enrollment.EnrollmentActionReasonId");

            Int32 enrollThroughSelection = Data.Readers.FormReader.Int32FromForm (form, "Sponsor_EnrollThroughSelection");

            Int64 enrollDirectProgramId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor_DirectProgramId");

            Int64 enrollParentSponsorId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.Enrollment.ParentSponsorId");

            Int64 enrollParentProgramId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.Enrollment.ParentProgramId");

            Int64 enrollProgramId = 0;


            if (coverageEffectiveDate.HasValue) {

                if (enrollThroughSelection == 1) { // IF DIRECT ENROLLMENT, MAKE SURE THAT THE VALUES ARE IGNORED 

                    enrollParentSponsorId = 0;

                    enrollParentProgramId = 0;

                    enrollProgramId = enrollDirectProgramId;

                }

                else { enrollProgramId = enrollParentProgramId; }

                success = Sponsor.Enroll (coverageEffectiveDate.Value, enrollmentActionReasonId, enrollParentSponsorId, enrollProgramId);

            }

            else {

                Application.SetLastException (new Exception ("Invalid coverage effective date provided."));

            }

            return success;

        }

        #endregion 

    }

}